//
//  ZMHomeOriginalView.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/26.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import SnapKit

class ZMHomeOriginalView: UIView {

    //  记录原创为微博底部约束
    var originalViewBottomConstraint: Constraint?
    
    //数据源
    var statusViewModel : ZMStatusViewModel? {
        didSet{
            
            //将模型数据解析显示再view上
            if let imageurl = statusViewModel?.statusesModel?.user?.profile_image_url {
                
                userimageView.sd_setImage(with: URL(string: imageurl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.statusesModel?.user?.screen_name
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
   
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedTypeImageview.image = statusViewModel?.verifiedTypeImage
            
            //设置来源
            sourceLabel.text = statusViewModel?.sourceContent
            
            //设置发布时间
            timeLabel.text = statusViewModel?.timecontent
            
            //删除之前的底部约束
            originalViewBottomConstraint?.deactivate()
            if let picurls = statusViewModel?.statusesModel?.pic_urls,picurls.count > 0 {
                
                //  1. 显示配图
                pictureView.isHidden = false
                //  2. 重新创建原创微博底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView.snp.bottom).offset(HomeCellMargin).constraint
                })
                //  3. 给原创微博配图绑定数据
                pictureView.picUrlArray = picurls
                
                
            }else {
                //  没有转发微博配图
                //  1. 隐藏配图
                pictureView.isHidden = true
                //  2. 重新创建原创微博底部约束
                self.snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin).constraint
                })
            }

            
            
            
            
        }
        
    }
    
    
    
    
    //懒加载控件 
    fileprivate lazy var userimageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    //认证类型图片
    fileprivate lazy var verifiedTypeImageview = UIImageView(image: #imageLiteral(resourceName: "avatar_vip"))
    //昵称
    fileprivate lazy var screenNameLabel : UILabel = {
    
        let label = UILabel(text: "趣闻搞笑", fontSize: 14, textColor: UIColor.darkGray)
        return label
        
    }()
    
    //时间
    fileprivate lazy var timeLabel : UILabel = {
       
        let label = UILabel(text: "刚刚", fontSize: 12, textColor: UIColor.orange)
        return label
    }()
    
    //来源
    fileprivate lazy var sourceLabel : UILabel = {
        
        let label = UILabel(text: "来自: 星星的你", fontSize: 12, textColor: UIColor.lightGray)
        return label
    }()
   //会员等级图片
    fileprivate lazy var mbrankImageView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "common_icon_membership"))
    
    //微博内容
    fileprivate lazy var contentLabel : UILabel = {
       
        let label = UILabel(text: "同行的老外在深圳买了一把大弹簧刀放行李箱在普通安检的时候被查到，保安队长也是一脸嫌弃，“外国人携带管制刀具交给警察手续也很繁琐的！算你自己丢弃吧！”我说如果是我的就简单了呗，直接带走拘留", fontSize: 13, textColor: UIColor.darkGray)
        
        //多行显示
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //  配图
    fileprivate lazy var pictureView: ZMHomeCellPictureView = {
        let collectionView = ZMHomeCellPictureView()
        //  配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
       //用户头像
        self.addSubview(userimageView)
        userimageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //认证类型
        self.addSubview(verifiedTypeImageview)
        verifiedTypeImageview.snp.makeConstraints { (make) in
            make.centerX.equalTo(userimageView.snp.trailing)
            make.centerY.equalTo(userimageView.snp.bottom)
        }
        
        //昵称
        self.addSubview(screenNameLabel)
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userimageView)
            make.leading.equalTo(userimageView.snp.trailing).offset(HomeCellMargin)
        }
        //时间
        self.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(userimageView)
        }
        //来源
        self.addSubview(sourceLabel)
        sourceLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        //会员等级图片
        self.addSubview(mbrankImageView)
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(HomeCellMargin)
            
        }
        //微博内同
        self.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userimageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userimageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        self.addSubview(pictureView)
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置默认大小，后续根据图片张数来确定真是的配图大小
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }

        //关键约束,,设置cell自动计算行高的约束
        self.snp.makeConstraints { (make) in
           originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
    
    
}
